// pages/gongxiu/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 'nf',
    hiddenshowguanzhu: false, //已关注和未关注（yiguanzhu weiguanzhu）的列表 不隐藏
    date_on: true, //今天
    qun: '大众共修',  //默认是大众共修
    header_image: 'http://www.hyhthjk.com.cn/static/nfbf/image/xcx/1.jpg', //黑板
    page: '1'  // 分页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获得openid
    var that = this;
    // that.getopenid();
    var status = that.data.status;
    var date_on = that.data.date_on;
    var qun = that.data.qun;  // 把哪个群的具体成员显示出来
    var page = that.data.page;
    // console.log(date_on)
    wx.request({
      url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Indexxcx/index3',
      data: {
        status: status, //把input的值给后台服务器
        openid: app.openid,
        date_on: date_on,
        qun: qun,  //默认是大众共修群
        page: page,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)

        that.setData({
          canyu_qun: res.data.canyu_qun,
          note: res.data.note,  //经藏偈语
          qun_men: res.data.qun_men,  //小群的成员
          yiguanzhu: res.data.yiguanzhu,  //已关注的
          weiguanzhu: res.data.weiguanzhu,  //没有关注的
          // hiddenqunchengyuan:true,
          hiddencanyu_qun: false,
          hiddensearch_qun: true,
        })

      }
    })
    // console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  search: function (e) {
    var that = this;
    //  console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var input = e.detail.value.input;
    console.log(input)
    wx.request({
      url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Indexxcx/index2',
      data: {
        data: input //把input的值给后台服务器
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          weiguanzhu: res.data.search_men,  //没有关注的
          search_qun: res.data.search_qun,
          hiddenshowyiguanzhu: true,  //隐藏已关注的成员
          hiddenqunchengyuan: true,  // 隐藏小群的成员
          hiddenshowweiguanzhu: false,
          hiddensearch_qun: false,
          hiddencanyu_qun: true,
        })
        //上面 setData 结束

      }
    })
  },

  /**
   * 当点击菜单时
   */
  setstyle: function (e) {
    var that = this;
    // console.log(e.currentTarget.dataset.class)
    var nav_class = e.currentTarget.dataset.class;
    that.setData({
      status: nav_class,
    });
    that.onShow();
  },
  /**
  * 当点击日期菜单时
  */
  date_on: function (e) {
    var that = this;
    console.log(that.data.date_on)
    var date_on = !that.data.date_on

    that.setData({
      date_on: date_on,
    })
    that.onShow();
  },

  /***
   * 当点击关注某人时 传给服务器 重加载 
   */
  guanzhu: function (e) {
    var that = this;
    var guanzhu = !guanzhu;
    wx.request({
      url: 'test.php', // 仅为示例，并非真实的接口地址
      data: {
        openid: '',
        //: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
      }
    })

  },
  /**
   * 退群
   */
  tuiqun: function (e) {
    var that = this;
    var qun = e.currentTarget.dataset.name;
    wx.showModal({
      title: '退群！',
      content: '您真的要退出"' + qun + '"吗',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.request_tuiqun(qun);

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /***
   * 显示群成员
   */

  show_qunmen: function (e) {

    var qun = e.currentTarget.dataset.name;
    //下面 if开始
    if (qun != '大众共修') {
      var hiddenshowyiguanzhu = true; //隐藏已关注的成员
      var hiddenqunchengyuan = false;  // 不能隐藏小群的成员
      var hiddenshowweiguanzhu = true;
    }
    else {
      var hiddenshowyiguanzhu = false;  //隐藏已关注的成员
      var hiddenqunchengyuan = true;  // 隐藏小群的成员
      var hiddenshowweiguanzhu = false;

    }
    //上面if 结束
    //  console.log(qun); 
    this.setData({
      qun: qun,
      canyu_qun: qun,
      hiddenshowyiguanzhu: hiddenshowyiguanzhu,
      hiddenqunchengyuan: hiddenqunchengyuan,
      hiddenshowweiguanzhu: hiddenshowweiguanzhu,
    });
    this.onShow();
  },

  /***
   * 
   */
  request_tuiqun: function (qun) {
    // console.log('向服务器要退群')
    var that = this;
    var openid = app.openid;
    var nickname = app.nickname;
    wx.request({
      url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Dltqunmen/index16', // 仅为示例，并非真实的接口地址
      data: {
        qun: qun,
        openid: openid,
        nickname: nickname,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        that.onShow();
      }
    })
  },

  /**
   * 在onload阶段 获得openid 
   */
  getopenid: function () {
    var that = this;

    //获得openid开始
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Indexxcx/index4',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              //  console.log(res.data)
              console.log('执行')
              app.get_openid = res.data;
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }

    })
    //获得openid 结束
  },

  /**
   * 申请加入群
   */
  add_qun: function (e) {
    var that = this;
    // console.log(e.currentTarget.dataset.name+app.openid+app.nickname)
    var openid = app.openid;
    var nickname = app.nickname;
    var name = e.currentTarget.dataset.name;
    wx.request({
      url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Indexxcx/index13', // 仅为示例，并非真实的接口地址
      data: {
        openid: openid,
        name: name,
        nickname: nickname,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        //返回群成员列表 刷新页面
        // console.log(res.data)
        that.onShow();
      }
    })

  },

  /**
   * 在共修大群里，加关注
   */
  geren_guanzhu: function (e) {
    var that = this;
    // console.log(e.currentTarget.dataset.openid +e.currentTarget.dataset.nickname + app.openid);
    var openid = app.openid;
    var nickname = e.currentTarget.dataset.nickname;
    var beiguanzhuopenid = e.currentTarget.dataset.openid  //被关注的人的openid
    wx.request({
      url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Addgerenguanzhu/index2', // 仅为示例，并非真实的接口地址
      data: {
        openid: openid,
        nickname: nickname,
        beiguanzhuopenid: beiguanzhuopenid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data)
        that.onShow();
      }
    })
  },

  /**
   * 个人关注个人 取消
   */
  geren_guanzhu_quxiao: function (e) {
    var that = this;
    var beiguanzhuopenid = e.currentTarget.dataset.openid;
    var openid = app.openid;
    console.log(beiguanzhuopenid + '****' + openid);
    wx.request({
      url: 'https://www.hyhthjk.com.cn/nfbf.php/index/Minusgerenguanzhu/index2', // 仅为示例，并非真实的接口地址
      data: {
        beiguanzhuopenid: beiguanzhuopenid,
        openid: openid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        that.onShow();
      }
    })
  },
beizhuname:function(e){
 // console.log(e.currentTarget.dataset.openid + e.currentTarget.dataset.nickname + app.openid);
  var beizhuopenid = e.currentTarget.dataset.openid;
  var beizhunickname = e.currentTarget.dataset.nickname;
  var beizhuurl = e.currentTarget.dataset.src;

  wx.navigateTo({
    url: '/pages/gongxiu/beizhuname/index'+'?beizhuopenid='+beizhuopenid+'&beizhunickname='+beizhunickname+'&beizhuurl='+beizhuurl
  })
}

})